package com.goldmantis.alitrip.controller.system;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.UserThreadLocal;
import com.goldmantis.alitrip.pojo.SysOrg;
import com.goldmantis.alitrip.service.SysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "组织架构管理")
@RequestMapping("sys/org")
@RequiresAuthentication

public class SysOrgController {

    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 获取组织架构
     * @param searchEntity
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "组织架构列表", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> list(@ApiParam("查询对象") SysOrg searchEntity) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysOrgService.select(searchEntity)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 根据Id获取组织架构(包含部门负责人和分管领导信息)
     * @param userId 用户Id
     * @return
     */
    @GetMapping(value = "{userId}")
    @ApiOperation(value = "根据Id获取组织架构(包含部门负责人和分管领导信息)", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键", required = true) @PathVariable(required = true) Long userId) {
        return ResponseEntity.ok(ReturnMessage.success(sysOrgService.getById(userId)));
    }

    /**
     * 新增组织架构
     * @param sysOrg
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增组织架构", httpMethod = "POST")
    public ResponseEntity<ReturnMessage> post(@ApiParam("组织架构对象") @RequestBody @Valid SysOrg sysOrg) {
            sysOrg.setCreatetime(new Date());
            sysOrg.setCreatorid(UserThreadLocal.get().getOrgId());
            return ResponseEntity.ok(ReturnMessage.success(sysOrgService.insert(sysOrg)));
    }

    /**
     * 编辑组织架构
     * @param sysOrg
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑组织架构", httpMethod = "PUT")
    public ResponseEntity<ReturnMessage> put(@ApiParam("组织架构对象") @RequestBody SysOrg sysOrg) {
        try {
            sysOrg.setCreatetime(new Date());
            sysOrg.setCreatorid(UserThreadLocal.get().getOrgId());
            return ResponseEntity.ok(ReturnMessage.success(sysOrgService.update(sysOrg)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 批量删除组织架构
     * @param org_ids
     * @return
     */
    @DeleteMapping(value = "{org_ids}")
    @ApiOperation(value = "批量删除组织架构", httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> delete(@ApiParam("组织架构id 逗号隔开（1,2）") @PathVariable String org_ids) {
        try {
            List<Object> ids = Arrays.asList(StringUtils.split(org_ids, ","));
            return ResponseEntity.ok(ReturnMessage.success(sysOrgService.deleteByIds(ids, SysOrg.class, "orgid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * @return
     */
    @GetMapping(value = "getOrgTree")
    @ApiOperation(value = "获取组织架构json", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getOrgTree() {
            return ResponseEntity.ok(ReturnMessage.success(sysOrgService.getSysTreeModel()));
    }

}
